package org.docshare.mm;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;

import org.docshare.zhuishu.R;

/**
 * Created by Administrator on 2016/5/14.
 */
public class YangDrawerLayout implements DrawerLayout.DrawerListener {

    private Activity context;
    private DrawerLayout mDrawerLayout;
    private View leftRoot;
    private MaterialMenuIcon mMaterialMenuIcon;
    private boolean isDirection_left;

    @SuppressLint("NewApi") public YangDrawerLayout(Activity context,DrawerLayout mDrawerLayout, View leftRoot){
        this.mDrawerLayout = mDrawerLayout;
        this.context = context;
        this.leftRoot = leftRoot;
        

        //设置DrawerLayout相关属性
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        // 设置ActionBar可见，并且切换菜单和内容视图
        context.getActionBar().setDisplayHomeAsUpEnabled(true);
        context.getActionBar().setHomeButtonEnabled(true);
        
        
        mMaterialMenuIcon = new MaterialMenuIcon(context, Color.WHITE, MaterialMenuDrawable.Stroke.THIN);
        mDrawerLayout.setDrawerListener(this);

    }

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        mMaterialMenuIcon.setTransformationOffset(
                MaterialMenuDrawable.AnimationState.BURGER_ARROW,
                isDirection_left ? 2 - slideOffset : slideOffset);

    }

    @Override
    public void onDrawerOpened(View drawerView) {
        isDirection_left = true;
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        isDirection_left = false;
    }

    @Override
    public void onDrawerStateChanged(int newState) {

    }


    /**
     * 点击ActionBar上菜单
     */
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                if (!isDirection_left) { // 左边栏菜单关闭时，打开
                    mDrawerLayout.openDrawer(leftRoot);
                } else {// 左边栏菜单打开时，关闭
                    mDrawerLayout.closeDrawer(leftRoot);
                }
                break;

            default:
                break;
        }
        return true;
    }
}
